#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N];
typedef int long long;
const int inf = 0x3f3f3f3f;
int ret = 0;
//分治
void slove(int begin, int end)
{
    if (begin >= end) {
        ret += a[begin];
        return;
    }
    int pos = -1;
    int min_num = inf; //最大值
    for (int i = begin; i <= end; i++)//寻找区间中的最大值
    {
        if (a[i] < min_num)  
        {
            min_num = a[i];//最小值
            pos = i;//记录最小值的下标
        }
    }
    ret += min_num;
    //处理数组
    for (int i = begin; i <= end; i++)
        a[i] -= min_num;//全部都减去最小值


    slove(begin, pos - 1);
    slove(pos + 1, end);
}

signed main()
{
    int n; cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    slove(0, n - 1);
    cout << ret << endl;
    return 0;
}
